package parser.impl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import parser.SourceAdapter;
import parser.UnParsableException;
import dto.RawSchemaDTO;

public class FileSourceAdapter implements SourceAdapter {

	private static final String FILE_NAME = "/Users/tchan/repo/elasticpath/cmates/trunk/cmates-db-scripts/oracle/schema/schema.sql";

	public RawSchemaDTO adapt() {
		try {
			return constructRawSchema(FILE_NAME);
		} catch (Exception e) {
			throw new UnParsableException(e.getMessage());
		}
	}

	public RawSchemaDTO constructRawSchema(final String filename)
	throws Exception
	{
		String line = null;
		List<String> records = new ArrayList<String>();

		// wrap a BufferedReader around FileReader
		BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));

		// use the readLine method of the BufferedReader to read one line at a time.
		// the readLine method returns null when there is nothing else to read.
		while ((line = bufferedReader.readLine()) != null)
		{
			records.add(line);
		}

		// close the BufferedReader when we're done
		bufferedReader.close();
		return construct(records);
	}

	private RawSchemaDTO construct(final List<String> records) {
		return new RawSchemaDTO(records);
	}

}
